{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "import subprocess"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "# specify the path to checkpoint here\n",
    "ckpt=\"/home/xudongw/UnSAM-Semantic/data/output/gpu4-bs4-lr1e-4-iter100k-12jsons-SSL-AllAnnos-NMask120-Thresh0.02/10k_0099999.pth\"\n",
    "# specify the path to config file here\n",
    "config=\"configs/semantic_sam_only_sa-1b_swinT.yaml\"\n",
    "# select cpu or gpu\n",
    "device=\"cpu\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/jingfeng/miniconda3/envs/UnSAM/lib/python3.8/site-packages/torch/functional.py:504: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:3483.)\n",
      "  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Loading pretrained SwinT model from:  /home/xudongw/UnSAM-Semantic/pretrained/moby_swin_t_300ep_pretrained.pth\n",
      "Missing keys:  ['layers.0.blocks.0.attn.relative_position_index', 'layers.0.blocks.1.attn.relative_position_index', 'layers.1.blocks.0.attn.relative_position_index', 'layers.1.blocks.1.attn.relative_position_index', 'layers.2.blocks.0.attn.relative_position_index', 'layers.2.blocks.1.attn.relative_position_index', 'layers.2.blocks.2.attn.relative_position_index', 'layers.2.blocks.3.attn.relative_position_index', 'layers.2.blocks.4.attn.relative_position_index', 'layers.2.blocks.5.attn.relative_position_index', 'layers.3.blocks.0.attn.relative_position_index', 'layers.3.blocks.1.attn.relative_position_index', 'norm0.weight', 'norm0.bias', 'norm1.weight', 'norm1.bias', 'norm2.weight', 'norm2.bias', 'norm3.weight', 'norm3.bias']\n",
      "pretrained_layers ['*']\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "$UNUSED$ criterion.empty_weight, Ckpt Shape: torch.Size([2])\n",
      "demo_promptable.py:101: GradioDeprecationWarning: The `style` method is deprecated. Please set these arguments in the constructor instead.\n",
      "  image=ImageMask(label=\"Click on Image (Please only click one point, or our model will take the center of all points as the clicked location. Remember to clear the click after each interaction, or we will take the center of the current click and previous ones as the clicked location.)\",type=\"pil\",brush_radius=15.0).style(height=512)\n",
      "demo_promptable.py:102: GradioDeprecationWarning: The `style` method is deprecated. Please set these arguments in the constructor instead.\n",
      "  gallery_output=gr.Gallery(label=\"Image Gallery sorted by IoU score.\",min_width=1536).style(grid=6)\n",
      "demo_promptable.py:102: GradioDeprecationWarning: The 'grid' parameter will be deprecated. Please use 'columns' in the constructor instead.\n",
      "  gallery_output=gr.Gallery(label=\"Image Gallery sorted by IoU score.\",min_width=1536).style(grid=6)\n",
      "demo_promptable.py:103: GradioDeprecationWarning: The `style` method is deprecated. Please set these arguments in the constructor instead.\n",
      "  gallery_output2=gr.Gallery(label=\"Image Gallery sorted by mask area.\",min_width=1536).style(grid=6)\n",
      "demo_promptable.py:103: GradioDeprecationWarning: The 'grid' parameter will be deprecated. Please use 'columns' in the constructor instead.\n",
      "  gallery_output2=gr.Gallery(label=\"Image Gallery sorted by mask area.\",min_width=1536).style(grid=6)\n",
      "demo_promptable.py:133: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 20.0}\n",
      "  with gr.Row(scale=20.0):\n",
      "demo_promptable.py:152: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 1.0}\n",
      "  with gr.Row(scale=1.0):\n",
      "demo_promptable.py:155: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 2.0}\n",
      "  with gr.Row(scale=2.0):\n",
      "demo_promptable.py:159: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 6.0}\n",
      "  with gr.Row(scale=6.0):\n",
      "demo_promptable.py:161: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 1.0}\n",
      "  with gr.Row(scale=1.0):\n",
      "demo_promptable.py:165: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 9.0}\n",
      "  with gr.Row(scale=9.0):\n",
      "demo_promptable.py:168: GradioUnusedKwargWarning: You have unused kwarg parameters in Row, please remove them: {'scale': 9.0}\n",
      "  with gr.Row(scale=9.0):\n",
      "Traceback (most recent call last):\n",
      "  File \"demo_promptable.py\", line 180, in <module>\n",
      "    demo.queue().launch(share=True,server_port=6082)\n",
      "  File \"/home/jingfeng/miniconda3/envs/UnSAM/lib/python3.8/site-packages/gradio/blocks.py\", line 1906, in launch\n",
      "    ) = networking.start_server(\n",
      "  File \"/home/jingfeng/miniconda3/envs/UnSAM/lib/python3.8/site-packages/gradio/networking.py\", line 207, in start_server\n",
      "    raise OSError(\n",
      "OSError: Cannot find empty port in range: 6082-6082. You can specify a different port by setting the GRADIO_SERVER_PORT environment variable or passing the `server_port` parameter to `launch()`.\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "IMPORTANT: You are using gradio version 3.42.0, however version 4.29.0 is available, please upgrade.\n",
      "--------\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "CompletedProcess(args='python demo_promptable.py                --ckpt /home/xudongw/UnSAM-Semantic/data/output/gpu4-bs4-lr1e-4-iter100k-12jsons-SSL-AllAnnos-NMask120-Thresh0.02/10k_0099999.pth                --conf_files configs/semantic_sam_only_sa-1b_swinT.yaml                --device cpu', returncode=1)"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# running this cell will pop up a gradio web link of interative demo, please keep the cell running during the demo\n",
    "subprocess.run(f\"python demo_promptable.py \\\n",
    "               --ckpt {ckpt} \\\n",
    "               --conf_files {config} \\\n",
    "               --device {device}\", shell=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "UnSAM",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
